/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.start.controller;

import com.itgrail.grail.cqrs.command.CommandBus;
import com.itgrail.grail.context.Context;
import com.itgrail.grail.dto.PageResult;
import com.itgrail.grail.dto.ResultData;

import com.itgrail.grail.extension.extension.ExtensionExecutor;
import com.itgrail.pilot.app.command.cmo.AddUserResultCmd;
import com.itgrail.pilot.app.command.cmo.DeleteUserCmd;
import com.itgrail.pilot.app.command.cmo.UpdateUserCmd;
import com.itgrail.pilot.app.command.cmo.UpdateUserPasswordCmd;
import com.itgrail.pilot.app.command.cmo.query.DetailUserCmd;
import com.itgrail.pilot.app.command.cmo.query.DetailUserShowCmd;
import com.itgrail.pilot.app.command.cmo.query.PageQueryUserCmd;
import com.itgrail.pilot.app.command.cmo.query.SyncUserCmd;
import com.itgrail.pilot.app.command.co.UserCO;
import com.itgrail.pilot.app.extension.UserExtPt;
import com.itgrail.pilot.infrastructure.tunnel.util.SessionUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xujin
 */
@RestController
@RequestMapping("/admin/user")
@Api("用户管理")
public class UserController {

    @Autowired
    private CommandBus commandBus;

    @Autowired
    private ExtensionExecutor executor;

    @ApiModelProperty("获取用户信息---提供给搜索用")
    @PostMapping("/getUserInfo")
    @ApiOperation(value = "获取用户信息")
    @RequiresPermissions("user:getUserInfo")
    public ResultData getUserInfo(@RequestBody DetailUserCmd cmd) {
        return commandBus.send(cmd);
    }

    @ApiModelProperty("登录成功后,调用接口,存入用户信息")
    @PostMapping("/loginSuccess")
    public ResultData loginSuccess(@RequestBody AddUserResultCmd cmd) {
        return commandBus.send(cmd);
    }

    @ApiModelProperty("获取用户信息---走本地扩展点")
    @PostMapping("/extensionExe")
    @ApiOperation(value = "获取用户信息走扩展")
    @RequiresPermissions("user:extensionExe")
    public ResultData exeExtension(@RequestBody DetailUserCmd cmd) {
        Context context = new Context();
        context.setBizCode("com.ali.ding");
        List<UserCO> list = executor.exeReturnValue(UserExtPt.class, context, e -> e.getUserInfo(cmd.getKey()));
        return ResultData.success(list);
    }

    @PostMapping("/page")
    @ApiOperation(value = "分页查询用户")
    public ResultData<PageResult<UserCO>> getUserPage(@RequestBody PageQueryUserCmd pageQueryUserCmd) {
        return commandBus.send(pageQueryUserCmd);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新用户")
    @RequiresPermissions("user:update")
    public ResultData<Void> updateUser(@RequestBody UpdateUserCmd updateUserCmd) {
        return commandBus.send(updateUserCmd);
    }

    @PostMapping("/delete")
    @ApiOperation(value = "删除用户")
    @RequiresPermissions("user:delete")
    public ResultData<Void> delete(@RequestBody DeleteUserCmd deleteUserCmd) {
        return commandBus.send(deleteUserCmd);
    }

    @PostMapping("/detail")
    @ApiOperation(value = "获取用户详情")
    public ResultData detail(@RequestBody DetailUserShowCmd cmd) {
        if (cmd.getCurrentUserId() == null) {
            cmd.setCurrentUserId(SessionUtils.currentUserId());
        }
        return commandBus.send(cmd);
    }

    @PostMapping("/sync")
    @ApiOperation(value = "同步用户")
    @RequiresPermissions("user:sync")
    public ResultData<Void> sync(@RequestBody SyncUserCmd cmd) {
        return commandBus.send(cmd);
    }

    @PostMapping("/updatePassword")
    @ApiOperation(value = "修改密码")
    @RequiresPermissions("user:updatePassword")
    public ResultData<Void> updatePassword(@RequestBody UpdateUserPasswordCmd updateUserPasswordCmd) {
        return commandBus.send(updateUserPasswordCmd);
    }

}
